//app.js
App({
    onLoad: function () {
      },
    onLaunch: function () {
        
        // 下面
       this.tartGlobalCountdown()
        const app = this; // 存储 this 到 app 变量
        setTimeout(()=>{
            const agreed = wx.getStorageSync('agreedPrivacy');
            app.globalData.agreed = true;
            app.globalData.showPopup = true;
       if (agreed) {
        app.globalData.agreed = false;
       }
        },0)

 setTimeout(()=>{
    const tokens=wx.getStorageSync('token')
    const rechargecontent=wx.getStorageSync('rechargecontent')
    if (tokens){
        app.globalData.canIUseGetUserProfiles=true
     }
   
 },0)
 
    },
    globalData: {
        userInfo: null,
        agreed:false,
        canIUseGetUserProfiles:false,
        showPopup:false,
        countdown:600, // 初始倒计时为30分钟，单位为秒1800
        formattedCountdown: '00:00', // 格式化后的倒计时
        formatcountdown:'倒计时'
    },
    //  api_url: 'https://kongjian.yuanchong.net',
    // api_url:'https://dy.yuanchong.net/' ,
    api_url: 'https://bjx.yuanchong.net/',
    ajax: function (o, show) {
        if (show) {
            wx.showLoading({
                mask: true,
                title: '正在加载...'
            });
        }
        var data = o.data || {};
        data.token = wx.getStorageSync('token') || '';
        wx.request({
            url: this.api_url + o.url,
            data: data,
            method: o.method || 'POST',
            header: o.header || {
                'content-type': 'application/json'
            },
            success: function (res) {
                if (show) {
                    wx.hideLoading();
                }
                if (res && res.data && res.data.code !== undefined) {
                    if (res.data.code == -1) {
                        console.log(res.data.msg,'用户未登录')
                        if(o.url=="/api/order/orderNotice"){
                            wx.showToast({
                                title: '用户未登录',
                                icon: 'none',
                                duration: 2000
                              });
                            console.log(res,this.api_url,o.url,'用户未登录')
                            setTimeout(()=>{
                                wx.navigateTo({
                                    url: '/pages/login/login',
                                 })
                            },1500)
                                            }
                        return false;
                    }
                    if (o.success) o.success(res.data);
                } else {
                    wx.showToast({
                        title: '请求错误',
                        icon: 'none'
                    });
                }
            },
            fail: function (res) {
                if (show) {
                    wx.hideLoading();
                }
                wx.showToast({
                    title: '请求错误',
                    icon: 'none'
                });
            }
        })
    },
    tartGlobalCountdown: function () {
        const app = this;
        const countdown = app.globalData.countdown;
        if (countdown <= 0) {
          this.callBackendAPI();
        } else {
          app.globalData.timerId = setInterval(() => {
            app.globalData.countdown -= 1;
            if (app.globalData.countdown <= 0) {
              clearInterval(app.globalData.timerId);
              app.globalData.formattedCountdown = this.formatCountdown(app.globalData.countdown);
              this.callBackendAPI();
            } else {
              let  order_id=wx.getStorageSync('order_id')
                console.log(app.globalData.countdown,'app.globalData.countdown--1')
                if(!order_id){
                    console.log(app.globalData.countdown,'app.globalData.countdown--2')
                               app.globalData.formatcountdown='00:00'
                               app.globalData.countdown=1
                    return 
                }
                
        
              app.globalData.countdown = app.globalData.countdown;
              app.globalData.formattedCountdown = this.formatCountdown(app.globalData.countdown);

            }
          }, 1000);
        }
      },
      formatCountdown: function (countdown) {
        const minutes = Math.floor(countdown / 60);
        const seconds = countdown % 60;
        return `${String(minutes).padStart(2, '0')}:${String(seconds).padStart(2, '0')}`;
      },
    
      callBackendAPI: function () {
        let order_ids=wx.getStorageSync('order_id')||0
        let status=wx.getStorageSync('status')||0
        if(!order_ids&&!status){
            console.log('!order_ids&&!status不执行后端接口的逻辑...')
            return
        }
     
        // 调用后端接口的逻辑...
        this.ajax({
            url: '/api/order/cancell',
            data: {
                order_id:order_ids // 这里替换成你的订单号
            },
            success: function(res) {
              // 取消订单成功的处理逻辑
              wx.showToast({
                title: res.msg,
                icon: 'none',
                success: function() {
                    wx.removeStorageSync('order_id')
                    wx.removeStorageSync('status')
                    wx.removeStorageSync('couponid')
                    wx.removeStorageSync('allcouponid')
                setTimeout(()=>{
                    wx.reLaunch({
                        url: '/pages/my_order/my_order'
                      });
                },1000)
              
                }
              });
            },
            fail: function(err) {
              // 取消订单失败的处理逻辑
            }
          });
       
        console.log('调用后端接口');
      },
      getHaerl:function(){
        const app = this;
        if(app.globalData.countdown==-1){
            app.globalData.countdown=0
        }
        // console.log(app.globalData.countdown,'app.globalData.countdown=20app.globalData.countdown=20app.globalData.countdow111')
        
      let order_ids=wx.getStorageSync('order_id')||0
      let status=wx.getStorageSync('status')||0
      setTimeout(()=>{
          if(order_ids&&status==1&& app.globalData.countdown==0){
            console.log(app.globalData.countdown,order_ids,status,'app.globalData.countdown倒计时')
              app.globalData.countdown=600
              this.tartGlobalCountdown()
              console.log('app.globalData.countdown=20app.globalData.countdown=20app.globalData.countdow222')
             }
            
      },0)

      }
})